package com.komorebi.friends.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.komorebi.friends.common.BaseResponse;
import com.komorebi.friends.common.ErrorCode;
import com.komorebi.friends.common.ResultUtils;
import com.komorebi.friends.exception.BusinessException;
import com.komorebi.friends.model.dto.groupMsg.GroupMsgQueryRequest;
import com.komorebi.friends.model.entity.GroupMsgContent;
import com.komorebi.friends.service.GroupMsgContentService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/groupMsgContent")
public class GroupMsgContentController {

    @Autowired
    GroupMsgContentService groupMsgContentService;

    //增删改查

    /**
     * 根据 id 获得消息
     * @param msgId
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<GroupMsgContent> getById(Long msgId, HttpServletRequest request) {
        if (msgId == null || msgId < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"消息id不合法");
        }
        GroupMsgContent msg = groupMsgContentService.getById(msgId);
        return ResultUtils.success(msg);
    }

    @PostMapping("/query")
    public BaseResponse<Page<GroupMsgContent>> listByPage(@RequestBody GroupMsgQueryRequest groupMsgQueryRequest, HttpServletRequest request) {
        if (groupMsgQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "查询请求不能为空");
        }
        int pageSize = groupMsgQueryRequest.getPageSize();
        int current = groupMsgQueryRequest.getCurrent();
        //限制爬虫
        if (pageSize > 20) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "页面大小过多");
        }
        Page<GroupMsgContent> msgContentPage = groupMsgContentService.page(new Page<>(current, pageSize),
                groupMsgContentService.getQueryMapper(groupMsgQueryRequest));
        return ResultUtils.success(msgContentPage);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteById(Long msgId, HttpServletRequest request) {
        if (msgId == null || msgId < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "消息id不合法");
        }
        boolean remove = groupMsgContentService.removeById(msgId);
        return ResultUtils.success(remove);
    }

    @PostMapping("/delete/list")
    public BaseResponse<Boolean> deleteByIds(List<Long> ids,HttpServletRequest request) {
        if (ids == null || ids.size() == 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "删除集合不能为空");
        }
        boolean remove = groupMsgContentService.removeByIds(ids);
        return ResultUtils.success(remove);
    }

}
